<?php

namespace App\Http\Controllers\Admin\AdminUsers\Api;

use DB;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

/**
 * @author mais <[<380354618@qq.com>]>
 * [对于Adminusers的数据操作]
 */
class AdminUsersApi extends Controller
{
	/**
	 * [查询一个表的全部数据]
	 * @author mais  <[<380354618@qq.com>]>
	 * @param  [str] $tab  [传入表名]
	 * @return [arr] array [这个表的所有数据]
	 */
	public function all($tab){
		$results = DB::select('select * from '.$tab);
		return $results;
	}


	/**
	 * [查询一个表的某些字段]
	 * @author mais <[<380354618@qq.com>]>
	 * @param  [str] $tab [传入表名]
	 * @param  [arr] $tit [该表的字段]
	 * @return [arr]      [这个表的这些字段的数据]
	 */
	public function select($tab,$tit){
		$str = '';

		foreach ($tit as $key => $value) {
			$str = $str.$value.',';
		}

		$newstr = substr($str,0,strlen($str)-1); 

		$results = DB::select('select '.$newstr.' from '.$tab);

		return $results;

	}

	/**
	 * 查询一个表的某些字段,并且分页
	 * @param  [str] $tab  [表名]
	 * @param  [array] $tit  [字段]
	 * @param  [int] $page [分页显示数]
	 * @return [array]       [返回数据]
	 */
	public function page($tab, $tit, $page){

		$users = DB::table($tab)->select($tit)->Paginate($page);

		return $users;

	}

	/**
	 * 用于后台添加用户方法,仅对于adminusers表生效
	 * @param [arr] $arr [传进来需要添加的数据]
	 * @return[int] 1or2 [添加成功则返回1,失败则返回2]
	 */
	public function add($arr){

		$res = DB::insert("insert into admin_users (uname, pass, tel, email, addtime, title, state ,created_at, updated_at) values (?, ?, ?, ?, ?, ?, ?, ? ,?)", [$arr['adminName'], md5(md5($arr['password'])), $arr['phone'], $arr['email'], time(), $arr['adminRole'], 0, date('y-m-d h:i:s', time()), date('y-m-d h:i:s', time())]);

		if($res){
			return 1;
		} else {
			return 2;
		}
	}

	/**
	 * 对应ID删除数据
	 * @return [int] 1or2 [1->成功,2->失败]
	 */
	public function del(){
		$res = DB::delete('delete from admin_users where id = '.$_GET['id']);
		if($res){
			echo 1;
		} else {
			echo 2;
		}
	}

	//禁用某个会员
	public function stop(){

		$res = DB::update('update admin_users set state = 1 where id = ?', [$_GET['id']]);
		if($res){
			echo 1;
		}
	}

	//启用某个会员
	public function start(){

		$res = DB::update('update admin_users set state = 0 where id = ?', [$_GET['id']]);
		if($res){
			echo 1;
		}
	}

	/**
	 * [用id找值]
	 * @param  [str] $tab [需要查找的表]
	 * @param  [int] $id  [需要查找的表\id]
	 * @return [arr]      [这个id的数据]
	 */
	public function find($tab, $id){

		$res = DB::select('select * from '.$tab.' where id = :id', ['id' => $id]);

		return $res;
	}
}
